# from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from label.models import Label
from label.serializers import LabelSerializer
# from note.models import Note


class LabelViewSet(ModelViewSet):
    """
    标签视图集

    list:
        获取全部标签
    create:
        新建标签
    retrieve:
        查看标签
    """

    queryset = Label.objects.all()
    serializer_class = LabelSerializer

    # def destroy(self, request, *args, **kwargs):
    #     """删除标签"""
    #
    #     # 获取标签
    #     label = self.get_object()
    #     label_id = str(label.id)
    #     # 获取可能含有该标签的笔记列表
    #     notes = Note.objects.filter(label_id_list__contains=label_id)
    #     # 遍历列表删除对应的标签
    #     for note in notes:
    #         # 分割标签字符串
    #         label_id_list = note.label_id_list.split('#')
    #         # 如果存在该标签则删除
    #         if label_id in label_id_list:
    #             label_id_list.remove(label_id)
    #             # 重新拼接标签字符串
    #             label_id_list_str = ''
    #             for li in label_id_list:
    #                 label_id_list_str += li + '#'
    #             note.label_id_list = label_id_list_str[:len(label_id_list_str)-1]
    #             note.save()
    #     # 删除该标签
    #     label.delete()
    #     return Response(status=204)
